<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class bill_uqcms extends control
{
    function __construct()
    {
        $this->spid = $_SESSION['shop']['spid'];
        parent::__construct();
    }
    function index()
    {
        $settle_time = cfg('shop_settle_time');
        $this->settle_frozen_price();
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $result = $this->db->get_all("select * from " . table('shop_bill') . " where shop_id = " . $this->spid . " order by receipt_time desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            $now_time = time();
            foreach ($result as $k => $v) {
                if ($v['settle_status'] == '0') {
                    $v['last_time'] = $now_time - $v['receipt_time'];
                }
                $v['receipt_time'] = _date($v['receipt_time']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('shop_bill') . " where shop_id = " . $this->spid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $time = time() - $settle_time;
        $sp_row = $this->db->get_one("select id,price from " . table('shop') . " where id = " . $this->spid);
        $sp_row['price'] = isset($sp_row['price']) ? $sp_row['price'] : '0.00';
        $sp_frozen = $this->db->get_one("select SUM(price_income) as income from " . table("shop_bill") . " where shop_id = " . $this->spid . " and settle_status = 0 and receipt_time > " . $time);
        $sp_row['frozen_price'] = isset($sp_frozen['income']) ? $sp_frozen['income'] : '0.00';
        $order_row = $this->db->get_one("select SUM(total_price) as price from " . table('order_info') . " where shop_id = " . $this->spid . " and order_status in(2,3)");
        $sp_row['order_price'] = isset($order_row['price']) ? $order_row['price'] : '0.00';
        $this->assign('sp_row', $sp_row);
        $this->display();
    }
    private function settle_frozen_price()
    {
        $count = 0;
        $loop = 0;
        $this->db->autocommit();
        $time = time() - cfg('shop_settle_time');
        $result_all = $this->db->get_all("select * from " . table("shop_bill") . " where shop_id = " . $this->spid . " and settle_status = 0 and receipt_time < " . $time . " and refund_status = 0");
        if ($result_all) {
            foreach ($result_all as $v) {
                $query = $this->db->query("update " . table('shop_bill') . " set settle_status = 1, settle_time = " . time() . " where id = " . $v['id']);
                $query2 = $this->db->query("update " . table('shop') . " set price = price + " . $v['price_income'] . " where id = " . $this->spid);
                if ($query && $query2) {
                    $count++;
                }
                $loop++;
            }
        }
        if ($loop == $count) {
            $this->db->commit();
        } else {
            $this->db->rollback();
        }
    }
    function withdraw()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $result = $this->db->get_all("select * from " . table('member_withdraw') . " where spid = " . $this->spid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('member_withdraw') . " where spid = " . $this->spid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function withdraw_edit()
    {
        $sp_row = $this->db->get_one("select id,price from " . table('shop') . " where id = " . $this->spid);
        if ($sp_row) {
            $this->assign('sp_row', $sp_row);
        }
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('member_withdraw') . " where spid = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $this->assign('row', $row);
            }
        }
        $this->display();
    }
    function withdraw_save()
    {
        if ($_POST) {
            $id = P('id', 0);
            if ($id) {
                $row = $this->db->get_one("select * from " . table('member_withdraw') . " where spid = " . $this->spid . " and id = " . $id);
                if ($row['id']) {
                    if ($row['pay_status'] == '1') {
                        error_json('已经提现成功');
                    } else {
                        $setarr['pay_type'] = P('pay_type', '支付地址不能为空');
                        $setarr['pay_account'] = P('pay_account', '支付账号不能为空');
                        $setarr['pay_name'] = P('pay_name', '支付姓名不能为空');
                        $setarr['pay_status'] = '0';
                        $query = $this->db->update(table('member_withdraw'), $setarr, " spid = " . $this->spid . " and id = " . $id);
                        if ($query) {
                            right_json();
                        } else {
                            error_json('保存错误');
                        }
                    }
                } else {
                    error_json('提交错误');
                }
            } else {
                $price = P('price', '', '0');
                if ($price > '0' && $price % 100 == '0') {
                    $sp_row = $this->db->get_one("select id,price from " . table('shop') . " where id = " . $this->spid);
                    if ($sp_row['price'] >= $price) {
                        $row = $this->db->get_one("select * from " . table('member_withdraw') . " where spid = " . $this->spid . " and pay_status = 0");
                        if (!$row['id']) {
                            $this->db->autocommit();
                            $surplus = $sp_row['price'] - $price;
                            $setarr['spid'] = $this->spid;
                            $setarr['price'] = $price;
                            $setarr['surplus'] = $surplus;
                            $setarr['pay_type'] = P('pay_type', '支付地址不能为空');
                            $setarr['pay_account'] = P('pay_account', '支付账号不能为空');
                            $setarr['pay_name'] = P('pay_name', '支付姓名不能为空');
                            $setarr['pay_remark'] = P('pay_remark');
                            $setarr['pay_status'] = '0';
                            $setarr['status'] = '1';
                            $setarr['addtime'] = time();
                            $query1 = $this->db->query("update " . table('shop') . " set price = price - " . $price . " where id = " . $this->spid);
                            $query2 = $this->db->add(table('member_withdraw'), $setarr);
                            if ($query1 && $query2) {
                                $this->db->commit();
                                right_json();
                            } else {
                                $this->db->rollback();
                                error_json('申请失败');
                            }
                        } else {
                            error_json('有正在进行中的申请，成功后再申请。');
                        }
                    } else {
                        error_json('账户金额不足');
                    }
                } else {
                    error_json('提现金额大于0，且为100的整数倍。');
                }
            }
        } else {
            error_json();
        }
    }
}